/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.biz.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.action.AbstractAction;
import com.mttang.framework.core.exception.MtTangServiceRuntimeException;
import com.mttang.framework.core.pojos.ServiceRequest;
import com.mttang.framework.core.pojos.ServiceResponse;
import com.mttang.sys.biz.service.MenuPrivilegeService;
import com.mttang.sys.biz.service.MenuService;
import com.mttang.sys.dto.MenuDTO;
import com.mttang.sys.dto.MenuPrivilegeDTO;
import com.mttang.sys.dto.TreeDTO;
import com.mttang.util.SysBeanConstant;
import com.mttang.util.TreeInfo;

/**
 * 类MenuAction.java的实现描述：菜单的管理TODO 类实现描述
 * 
 * @author acc 2010-6-1 上午11:26:27
 */
public class MenuAction extends AbstractAction {

    private Log logger = LogFactory.getLog(getClass());

    private MenuService service;
    
    public void setService(MenuService service) {
		this.service = service;
	}

	public ServiceResponse perform(ServiceRequest request) throws MtTangServiceRuntimeException {
		return null;
	}
    
    public Map<Object,Object> left(Map<String,Object> map){
    
    	Map<String, Object> paramLevel2 = new HashMap<String, Object>();
        paramLevel2.put("roleId",  String.valueOf(map.get("roleId")));
        paramLevel2.put("menuLevel", 2);
        StringBuilder str = new StringBuilder();
        List<MenuDTO> level2Menus = service.findMenuLeft(paramLevel2);
        if (level2Menus != null && level2Menus.size() > 0) {
            for (int i = 0; i < level2Menus.size(); i++) {
                str.append("t=outlookbar.addtitle('").append(level2Menus.get(i).getMenuName())
                    .append("','").append(level2Menus.get(i).getMenuPid()).append("',1);\r\n   ");
                Map<String, Object> paramLevel3 = new HashMap<String, Object>();
                paramLevel3.put("roleId", String.valueOf(map.get("roleId")));
                paramLevel3.put("menuLevel", 3);
                paramLevel3.put("menuPid", level2Menus.get(i).getMenuId());
                List<MenuDTO> level3Menus = service.findMenuLeft(paramLevel3);
                if (level3Menus != null && level3Menus.size() > 0) {
                    for (int j = 0; j < level3Menus.size(); j++) {
                        str.append("outlookbar.additem('").append(level3Menus.get(j).getMenuName())
                        .append("',t,'").append(level3Menus.get(j).getMenuUrl()).append("&prvmenuId=").append(level3Menus.get(j).getMenuId()).append("');\r\n   ");
                    }
                }
            }
        }

        Map<Object, Object> map2 = new HashMap<Object, Object>();
        map2.put("str", str);
        return map2;
    }
    
    public Map<Object,Object> top(Map<String,Object> map){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("roleId", String.valueOf(map.get("roleId")));
        param.put("menuPid", -1);
        List<MenuDTO> list = service.findMenuTop(param);
        Map<Object, Object> map2 = new HashMap<Object, Object>();
        map2.put("list", list);
        return map2;
    }
    
    public Map<Object,Object> query(Map<String,Object> map2){

        Map<String, Object> param = new HashMap<String, Object>();
        List<MenuDTO> list = service.findMenu(param);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List<TreeDTO> tlist = new ArrayList<TreeDTO>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                TreeDTO treedto = new TreeDTO();
                treedto.setId(list.get(i).getMenuId());
                treedto.setName(list.get(i).getMenuName());
                treedto.setPid(list.get(i).getMenuPid());
                tlist.add(treedto);
            }
        }
        String str = new TreeInfo().getTreeStr(tlist);
        map.put("str", str);
        return map;
    
    }
    
    @SuppressWarnings("unchecked")
	public Map<Object,Object> add(Map<String,Object> map2){

        MenuDTO dto = new MenuDTO();
        dto.setMenuName(String.valueOf(map2.get("menuName")));
        dto.setMenuPid((Integer) map2.get("menuPid"));
        dto.setMenuUrl(String.valueOf(map2.get("menuUrl")));
        dto.setShowlevel((Integer) map2.get("showlevel"));
        dto.setPrv((String[]) map2.get("prvId"));
        if (String.valueOf(map2.get("menuPid")).equals("-1")) {
            dto.setMenuLevel(1);
        } else {
            MenuDTO mm = service.findById((Integer) map2.get("menuPid"));
            dto.setMenuLevel(mm.getMenuLevel() + 1);
        }
        Map<Object, Object> logMap = (Map<Object, Object>)map2.get("LOGMAP");
        service.insert(dto,logMap);
        return null;
    }
    

    public Map<Object,Object> update(Map<String,Object> map2){
        MenuDTO dto = new MenuDTO();
        dto.setMenuName(String.valueOf(map2.get("menuName")));
        dto.setMenuUrl(String.valueOf(map2.get("menuUrl")));
        dto.setShowlevel((Integer) map2.get("showlevel"));
        dto.setMenuId((Integer) map2.get("menuId"));
        dto.setPrv((String[]) map2.get("prvId"));
        Map<Object, Object> logMap = (Map<Object, Object>)map2.get("LOGMAP");
        service.update(dto,logMap);
        return null;
    }
    
    @SuppressWarnings("unchecked")
	public Map<Object,Object> delete (Map<String,Object> map2){
        MenuDTO dto = new MenuDTO();
        Long delete = service.findByPid((Integer) map2.get("menuId"));
        if(delete>0){
            Map<Object, Object> map = new HashMap<Object, Object>();
            map.put("menuid", "1");
            return map;
        }else{
            dto.setMenuId((Integer) map2.get("menuId"));
            Map<Object, Object> logMap = (Map<Object, Object>)map2.get("LOGMAP");
            service.delete(dto,logMap);
            return null;
        }
    }
    
    public Map<Object,Object> searchById(Map<String,Object> map2){
        MenuDTO dto = service.findById((Integer) map2.get("menuId"));
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("menu", dto);
        return map;
    }
    
    public Map<Object,Object> menuadd(Map<String,Object> map2){
        MenuPrivilegeService prvService = (MenuPrivilegeService) context.getBean(SysBeanConstant.SERVICE_MENU_PRIVILEGE);
        Integer menuId =  Integer.parseInt(String.valueOf(map2.get("menuId")));
        List<MenuPrivilegeDTO> list = prvService.findById(menuId);
        MenuDTO dto = service.findById(menuId);
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("menu", dto);
        map.put("prvlist", list);
        return map;
    }
    
    
    
}
